/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package hearthstonemenu;

import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.ListModel;

/**
 *
 * @author Bartu
 */
public class ViewDeckMenu extends javax.swing.JPanel {

    int pageNo = 0;
    ArrayList<JButton> selectedCardButtons;
    /**
     * Creates new form ViewDeckMenu
     */
    public ViewDeckMenu() {
        initComponents();
        showCards();
        selectedCardButtons = new ArrayList<JButton>();
    }

    public void showCards()
    {
        
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cardButton2 = new javax.swing.JButton();
        cardButton3 = new javax.swing.JButton();
        cardButton4 = new javax.swing.JButton();
        cardButton1 = new javax.swing.JButton();
        cardButton6 = new javax.swing.JButton();
        cardButton5 = new javax.swing.JButton();
        nextCardsButton = new javax.swing.JButton();
        backCardsButton = new javax.swing.JButton();
        newDeckButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        selectedCardList = new javax.swing.JList();

        cardButton2.setEnabled(false);
        cardButton2.setName("cardButton2"); // NOI18N
        cardButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cardButton1ActionPerformed(evt);
            }
        });

        cardButton3.setEnabled(false);
        cardButton3.setName("cardButton3"); // NOI18N
        cardButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cardButton1ActionPerformed(evt);
            }
        });

        cardButton4.setEnabled(false);
        cardButton4.setName("cardButton4"); // NOI18N
        cardButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cardButton1ActionPerformed(evt);
            }
        });

        cardButton1.setEnabled(false);
        cardButton1.setName("cardButton1"); // NOI18N
        cardButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cardButton1ActionPerformed(evt);
            }
        });

        cardButton6.setEnabled(false);
        cardButton6.setName("cardButton6"); // NOI18N
        cardButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cardButton1ActionPerformed(evt);
            }
        });

        cardButton5.setEnabled(false);
        cardButton5.setName("cardButton5"); // NOI18N
        cardButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cardButton1ActionPerformed(evt);
            }
        });

        nextCardsButton.setText("Next");
        nextCardsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextCardsButtonActionPerformed(evt);
            }
        });

        backCardsButton.setText("Back");
        backCardsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backCardsButtonActionPerformed(evt);
            }
        });

        newDeckButton.setText("New Deck");
        newDeckButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDeckButtonActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(selectedCardList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(newDeckButton)
                .addGap(40, 40, 40))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(backCardsButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cardButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cardButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cardButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(50, 50, 50)
                        .addComponent(cardButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cardButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cardButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nextCardsButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(174, 174, 174)
                                .addComponent(nextCardsButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(176, 176, 176)
                                .addComponent(backCardsButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(cardButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cardButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cardButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(42, 42, 42)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cardButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(cardButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(cardButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(32, 32, 32))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)))
                .addComponent(newDeckButton, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void backCardsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backCardsButtonActionPerformed
        pageNo--;
        showCards();
    }//GEN-LAST:event_backCardsButtonActionPerformed

    private void nextCardsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextCardsButtonActionPerformed
        pageNo++;
        showCards();
    }//GEN-LAST:event_nextCardsButtonActionPerformed

    private void newDeckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newDeckButtonActionPerformed
        if(newDeckButton.getText().equals("New Deck"))
        {
            newDeckButton.setText("Finish Deck");
            
            cardButton1.setEnabled(true);
            cardButton2.setEnabled(true);
            cardButton3.setEnabled(true);
            cardButton4.setEnabled(true);
            cardButton5.setEnabled(true);
            cardButton6.setEnabled(true);
            
        }
        else
        {
            if(selectedCardButtons.size() < 4)
                JOptionPane.showMessageDialog(null, "Your deck should have 30 cards, please add " + (30 - selectedCardButtons.size()) + " cards.", "Insufficient Cards", JOptionPane.INFORMATION_MESSAGE);
            else
            {
                newDeckButton.setText("New Deck");

                cardButton1.setEnabled(false);
                cardButton2.setEnabled(false);
                cardButton3.setEnabled(false);
                cardButton4.setEnabled(false);
                cardButton5.setEnabled(false);
                cardButton6.setEnabled(false);
                
                // Kayıt etme yazılacak.
                
                DefaultListModel model = new DefaultListModel();
                selectedCardList.setModel(model);
                selectedCardButtons.clear();
            }
        }
    }//GEN-LAST:event_newDeckButtonActionPerformed

    private void cardButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cardButton1ActionPerformed
        // TODO add your handling code here:
        if(selectedCardButtons.size() < 4)
        {
            JButton pressed = ((JButton)evt.getSource());
            DefaultListModel model = new DefaultListModel();
            for(int i = 0; i < selectedCardButtons.size(); i++)
            {
                if(selectedCardList.getModel().getElementAt(i) == pressed.getName())
                {
                    JOptionPane.showMessageDialog(null, "A deck can only have one type of any card, you cannot add the same card.", "Maximum Card", JOptionPane.INFORMATION_MESSAGE);
                    return;
                }
                model.addElement(selectedCardList.getModel().getElementAt(i));
            }
            model.addElement(pressed.getName());
            selectedCardList.setModel(model);
            selectedCardButtons.add(pressed);
        }
        else
            JOptionPane.showMessageDialog(null, "Your deck has 30 cards, you cannot add more cards.", "Maximum Cards", JOptionPane.INFORMATION_MESSAGE);

    }//GEN-LAST:event_cardButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backCardsButton;
    private javax.swing.JButton cardButton1;
    private javax.swing.JButton cardButton2;
    private javax.swing.JButton cardButton3;
    private javax.swing.JButton cardButton4;
    private javax.swing.JButton cardButton5;
    private javax.swing.JButton cardButton6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton newDeckButton;
    private javax.swing.JButton nextCardsButton;
    private javax.swing.JList selectedCardList;
    // End of variables declaration//GEN-END:variables
}
